﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GameSchool.Models;

namespace GameSchool.Controllers
{
    public class TeacherController : Controller
    {
		GameSchoolRepository repository = new GameSchoolRepository();
        //
        // GET: /Teacher/
        #region Views

        //For teachers
		public ActionResult TeacherHome()
        {
			var viewmodel = new TeacherPartialViewModel();
			var teacherLogin = repository.GetTeacherLoginRelationship();

			viewmodel.teacherName = teacherLogin.name; //For the top bar

			viewmodel.sidebarCourses = repository.GetCoursesByTeacher(teacherLogin).ToList();

			return View(viewmodel);
        }

        public ActionResult TeacherCourseHome(int ID)
        {
            
            var viewmodel = new TeacherCourseViewModel();
            
            var teacherLogin = repository.GetTeacherLoginRelationship();
            


            var theCourse = repository.GetCourseByID(ID);

            viewmodel.levels = repository.GetAllLevelsByCourse(theCourse); //getting levels for a particular course.
            viewmodel.sidebarCourses = repository.GetCoursesByTeacher(teacherLogin).ToList(); //for sidebar.
            viewmodel.teacherName = teacherLogin.name; //for titlebar.
            viewmodel.listCourses = repository.GetCoursesByTeacher(teacherLogin).ToList();
            viewmodel.courseName = theCourse.title;
            viewmodel.courseID = theCourse.courseID;

            return View(viewmodel);
        }

        //For supervisors        
        public ActionResult CreateTeacher()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreateTeacher(FormCollection formData)  // Ólöf
        {
			Teacher t = new Teacher();

			t.name = formData["name"];
			t.kennitala = Convert.ToInt32(formData["kennitala"]);
			t.address = formData["address"];
			t.zip = Convert.ToInt32(formData["zip"]);
			t.phone = Convert.ToInt32(formData["phone"]);
			t.email = formData["email"];

			repository.AddTeacher(t);
			repository.Save();

			return Redirect("~/Supervisor/SupervisorHome"); 
        }

        //For supervisors
        //Displays form with the fields filled in with data from the database
        public ActionResult TeacherEdit(int id)
        {
            // TODO if we have time - Ólöf
			return View();
        }

        //Used to write data to the database if anything was changed in a teacher form that already has data in it
        [HttpPost]
        public ActionResult TeacherEdit(int id, FormCollection formData)
        {
			// TODO if we have time - Ólöf
			return View();
        }

        //For supervisors
        [HttpPost]
        public ActionResult RemoveTeacher(int id)
        {
            return View();
        }
        #endregion

        
		public ActionResult XPAsignment(int teacherID, int stuID, int assID, int courseID)
		{
            TeacherXPAssignViewModel viewmodel = new TeacherXPAssignViewModel();
            var teacherLogin = repository.GetTeacherLoginRelationship();

            Course theCourse = repository.GetCourseByID(courseID);
            viewmodel.sidebarCourses = repository.GetCoursesByTeacher(teacherLogin).ToList(); //for sidebar.
            viewmodel.teacherName = teacherLogin.name; //for titlebar.
            viewmodel.courseName = theCourse.title;
            viewmodel.courseID = theCourse.courseID;
            viewmodel.teacherID = teacherID;
            viewmodel.assID = assID;
            viewmodel.stuID = stuID;

            
            Assignment ass = repository.GetAssignmentByID(assID);
            viewmodel.assXP = ass.assignmentXP;

            var student = repository.GetStudents(stuID);
            viewmodel.studentName = student.name;
                 

			return View(viewmodel);
		}

        [HttpPost]
        public ActionResult XPAsignment(FormCollection formData)
        {
            GameSchoolContext db = new GameSchoolContext();

            StudentCourse sc = new StudentCourse();


            sc.courseID = Convert.ToInt32(formData["courseID"]);
            sc.studentID = Convert.ToInt32(formData["stuID"]);

            int assXP = Convert.ToInt32(formData["assXP"]);

            int currentXPInCourse = (from l in db.studentCourses
                                 where sc.courseID == l.courseID && sc.studentID == l.studentID
                                 select l.studentXP).SingleOrDefault();

            sc.studentXP = currentXPInCourse + assXP;

            repository.UpdateStudentXP(sc);
            repository.Save();
            return RedirectToAction("../Assignment/listAssignments", new { CID = sc.courseID });
           
        }

        public Notification GetNotifications(Notification theNotification)
        {
            return theNotification;
        }
    }
}
